/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPPartLoadedCallback;
import fr.openparts.OpenParts.client.OPPartLoadingRequest;

/**
 * GWT tests for OPPartLoadingRequest.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 24 January 2010
 */
public class OPPartLoadingRequestTestGwt extends GWTTestCase {

    private static final String SERVICE_NAME = "dummyService_partloadingrequest";

    private OPPartLoadingRequest request;
    private OPPartLoadedCallback callback;

    private int onSuccessCalled = 0;
    private int onTimeoutCalled = 0;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the action.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        callback = new OPPartLoadedCallback() {
            @Override
            public void onTimeout() {
                ++onTimeoutCalled;
            }

            @Override
            public void onSuccess() {
                ++onSuccessCalled;
            }
        };
        request = OPPartLoadingRequest.partLoadingRequestFactory(SERVICE_NAME, callback);
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        onSuccessCalled = 0;
        onTimeoutCalled = 0;
    }

    /**
     * Test creating a part loading request. Should get something not null.
     */
    public final void testCreatingPartLoadingRequest() {
        assertNotNull("part loading request shouldn't be null", request);
    }

    /**
     * Test trying to create a part loading request with a null service. Should raise an assertion error.
     */
    public final void testCreatingPartLoadingRequestWithNullService() {
        if (assertionsAreEnabled()) {
            try {
                request = OPPartLoadingRequest.partLoadingRequestFactory(null, callback);
                fail("OPPartLoadingRequest should raise an assertion error when trying to build a request with a null service.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test trying to create a part loading request with a null callback. Should NOT raise an error.
     */
    public final void testCreatingPartLoadingRequestWithNullCallback() {
        try {
            request = OPPartLoadingRequest.partLoadingRequestFactory(SERVICE_NAME, null);
        } catch (Throwable e) {
            fail("OPPartLoadingRequest should NOT raise an error when trying to build a request with a null callback.");
        }
    }

    /**
     * Test getting back the service name.
     */
    public final void testGettingBackServiceName() {
        assertEquals("Service should be " + SERVICE_NAME, SERVICE_NAME, request.getService());
    }
    
    /**
     * Test getting back initial synchronous state.
     */
    public final void testGettingBackInitialSynchronousState() {
        assertFalse("Request should not be synchronous by default", request.wasExecutedSynchronously());
    }
    
    /**
     * Test setting the request as synchronous.
     */
    public final void testSettingSynchronousState() {
        assertFalse("Request should not be synchronous by default", request.wasExecutedSynchronously());
        request.setAsHavingBeenExecutedSynchronously();
        assertTrue("Request should now be synchronous", request.wasExecutedSynchronously());
    }
    
    /**
     * Test calling onSuccess method. Should call the callback onSuccess method.
     */
    public final void testCallingOnSuccess() {
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onTimeout should not have been called yet", 0, onTimeoutCalled);
        request.onSuccess();
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onTimeout should still not have been called", 0, onTimeoutCalled);
    }

    /**
     * Test calling onTimeout method. Should call the callback onTimeout method.
     */
    public final void testCallingOnTimeout() {
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onTimeout should not have been called yet", 0, onTimeoutCalled);
        request.onTimeout();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onTimeout should have been called now", 1, onTimeoutCalled);
    }

    /**
     * Test calling onSuccess method with a null callback. Should NOT raise an error.
     */
    public final void testCallingOnSuccessWithNullCallback() {
        try {
            request = OPPartLoadingRequest.partLoadingRequestFactory(SERVICE_NAME, null);
            request.onSuccess();
        } catch (Throwable e) {
            fail("OPPartLoadingRequest should NOT raise an error when calling onSuccess() with a null callback.");
        }
    }

    /**
     * Test calling onTimeout method with a null callback. Should NOT raise an error.
     */
    public final void testCallingOnTimeoutWithNullCallback() {
        try {
            request = OPPartLoadingRequest.partLoadingRequestFactory(SERVICE_NAME, null);
            request.onTimeout();
        } catch (Throwable e) {
            fail("OPPartLoadingRequest should NOT raise an error when calling onTimeout() with a null callback.");
        }
    }

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
}
